package Compation;

public class longestDiverseString {
    String res = "";

    int sum;

    int maxLength = 0;

    public static void main(String[] args) {
        int a = 4;
        int b = 42;
        int c = 7;
        System.out.println(new longestDiverseString().longestDiverseString(a, b, c));
    }

    public String longestDiverseString(int a, int b, int c) {
        backTrack("", a, b, c);
        this.sum = a + b + c;
        return res;
    }

    public void backTrack(String curRes, int a, int b, int c) {
        if (curRes.length() > maxLength) {
            res = curRes;
            maxLength = res.length();
        }
        if (a > 0 && isValid(curRes + "a")) {
            backTrack(curRes + "a", a - 1, b, c);
        }
        if (b > 0 && isValid(curRes + "b")) {
            backTrack(curRes + "b", a, b - 1, c);
        }
        if (c > 0 && isValid(curRes + "c")) {
            backTrack(curRes + "c", a, b, c - 1);
        }
    }

    private boolean isValid(String res) {
        if (res.contains("aaa") || res.contains("bbb") || res.contains("ccc")) {
            return false;
        }
        return true;
    }
}
